package GA;

public class Binary 
{
	int[] binary = new int[size];
	int integer;
	public static final int size = 4;
	public static final int intValue = 15;
	
	public Binary(int value)
	{
		if(value >=  Math.pow((double)2, (double)size))
			return;
		
		int temp = value;
		for(int i = size-1; i >= 0 && i < size && temp != 0; i--)
		{
			if(temp - Math.pow((double)2, (double)i) >= 0)
			{
				binary[(size-1)-i] = 1;
				temp = (int) (temp - Math.pow((double)2, (double)i));
			}
			else
			{
				binary[(size-1)-i] = 0;
			}
		}
		integer = value;
	}
	
	public int[] get()
	{
		return binary;
	}
	
	/**
	 * It gets the Integer value that the binary symbolize.
	 * @return Integer  from 0 to intValue (Static final value in Binary).
	 */
	public int getInteger()
	{
		return integer;
	}
	
	public void correct()
	{
		calculateInteger();
	}
	
	private void calculateInteger()
	{
		int temp = 0;

		for(int i = size-1; i >= 0 && i < size; i--)
		{
			if(binary[(size-1)-i] == 1)
				temp = (int)(temp + Math.pow((double)2, (double)i));
		}
		
		integer = temp;
	}

	public void invert(int i) 
	{
		binary[i] = binary[i]==1?0:1;
		
		calculateInteger();
	}

	public int get(int i)
	{
		return (i<Binary.size && i>=0)?binary[i]:null;
	}

}
